package com.kamloo.stg.fragment;

import java.util.ArrayList;

import com.kamloo.stg.adapter.TabBusScheduleArrayAdapter;
import com.kamloo.stg.data.db.*;
import com.kamloo.stg.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TabBusStopSchedule extends Fragment {
	private int m_nodeId;
	private int m_periodId;	
	private boolean m_isBookmark;
	private TextView m_scheduleLine;
	private TextView m_scheduleStop;
	private TextView m_schedulePeriod;
	private ListView m_scheduleList;
	private TextView m_scheduleNoData;	
	private ImageButton m_scheduleBookmark;
	ArrayList<String> m_values;
	
	OnClickListener bookmarkClickListener = new OnClickListener() {		
		public void onClick(View view) {
			BookmarkDAO bookmarkDao = new BookmarkDAO(getActivity());
			bookmarkDao.open();
			
			if (m_isBookmark) {
				Bookmark bookmark = bookmarkDao.select(m_nodeId);
				bookmarkDao.delete(bookmark.getId());
				m_scheduleBookmark.setImageResource(R.drawable.alarm_bw);
				m_isBookmark = false;
			} else {
				bookmarkDao.add(m_nodeId);
				m_scheduleBookmark.setImageResource(R.drawable.alarm);
				m_isBookmark = true;
			}
			
			bookmarkDao.close();
		}		
	};
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (container == null) {
			return null;
		} 
		return (RelativeLayout) inflater.inflate(R.layout.tab_bus_schedule, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		m_scheduleList = (ListView) view.findViewById(R.id.tabBusStopScheduleList);
		m_scheduleNoData = (TextView) view.findViewById(R.id.tabBusStopScheduleNoData);
		m_scheduleLine = (TextView) view.findViewById(R.id.tabBusStopScheduleLineInfo);
		m_scheduleStop = (TextView) view.findViewById(R.id.tabBusStopScheduleStopInfo);		
		m_schedulePeriod = (TextView) view.findViewById(R.id.tabBusStopSchedulePeriodInfo);
		m_scheduleBookmark = (ImageButton) view.findViewById(R.id.tabBusStopScheduleFavorite);
	}
	
	public void initializeView(int nodeId, int periodId) {
		m_nodeId = nodeId;
		m_periodId = periodId;
		m_values = new ArrayList<String>();
		
		setupBookmark();
		setScheduleData();
		
		if (m_values.size() != 0) {
			TabBusScheduleArrayAdapter adapter = new TabBusScheduleArrayAdapter(getActivity(), m_values.toArray(new String[m_values.size()]));
			m_scheduleList.setAdapter(adapter);

			m_scheduleLine.setVisibility(View.VISIBLE);
			m_scheduleLine.setText(getResources().getString(R.string.tabBus_Schedule_LineInfo) + " " + getLineName());
			m_scheduleStop.setVisibility(View.VISIBLE);
			m_scheduleStop.setText(getResources().getString(R.string.tabBus_Schedule_StopInfo) + " " + getStopName());
			m_schedulePeriod.setVisibility(View.VISIBLE);
			m_schedulePeriod.setText(getResources().getString(R.string.tabBus_Schedule_PeriodInfo) + " " + getPeriodName());
			m_scheduleBookmark.setVisibility(View.VISIBLE);
			m_scheduleBookmark.setOnClickListener(bookmarkClickListener);

		} else {
			m_scheduleNoData.setVisibility(View.VISIBLE);
		}		
	}
	
	private void setupBookmark() {
		BookmarkDAO bookmarkDao = new BookmarkDAO(getActivity());
		bookmarkDao.open();		
		m_isBookmark = bookmarkDao.isFavorite(m_nodeId);	
		if (m_isBookmark) {
			m_scheduleBookmark.setImageResource(R.drawable.alarm);
		}
		else {
			m_scheduleBookmark.setImageResource(R.drawable.alarm_bw);
		}
		bookmarkDao.close();
	}
	
	private String getStopName() {
		NodeDAO nodeDao = new NodeDAO(getActivity());
		StopDAO stopDao = new StopDAO(getActivity());
		nodeDao.open();
		stopDao.open();
		
		Node node = nodeDao.select(m_nodeId);
		Stop stop = stopDao.select(node.getFk_stopId());
		
		stopDao.close();
		nodeDao.close();
		
		return stop.getName();
	}
	
	private String getLineName() {
		NodeDAO nodeDao = new NodeDAO(getActivity());
		LineDAO lineDao = new LineDAO(getActivity());
		DirectionDAO directionDao = new DirectionDAO(getActivity());
		nodeDao.open();
		lineDao.open();
		directionDao.open();
		
		Node node = nodeDao.select(m_nodeId);
		Direction direction = directionDao.select(node.getFk_directionId());
		Line line = lineDao.select(direction.getFk_lineId());
		
		directionDao.close();
		lineDao.close();
		nodeDao.close();
		
		String name = line.getIdentifier();
		if (name.contains("STS"))
	    {
			name = name.replace("STS", "");
			name = name.substring(0, name.indexOf("_"));
	    }
		
		return name;
	}
	
	private String getPeriodName() {
		//PeriodDAO periodDao = new PeriodDAO(getActivity());
		//periodDao.open();
		//Period period = periodDao.select(m_periodId);
		//periodDao.close();		
		//return period.getName();
		
		switch (m_periodId) {
		case 0:
			return getString(R.string.period_Monday_Friday);
		case 1:
			return getString(R.string.period_Summer);
		case 2:
			return getString(R.string.period_Saturday);
		case 3: 
			return getString(R.string.period_Sunday);
		}
		
		return "";
	}
	
	private void setScheduleData() {
		ScheduleDAO scheduleDao = new ScheduleDAO(getActivity());
    	scheduleDao.open();
    	
    	ArrayList<Schedule> schedules = scheduleDao.select(m_nodeId, m_periodId);;
    	for (Schedule schedule : schedules) {
    		
    		String value = "";
			if (schedule.getHour() < 10) {
				value += "0" + schedule.getHour();
			} else {
				value += schedule.getHour();
			}
			value += ":";
			if (schedule.getMinute() < 10) {
				value += "0" + schedule.getMinute();
			} else {
				value += schedule.getMinute();
			}
			
    		m_values.add(value);    		
    	}
    	
    	scheduleDao.close();
	}
	
}
	


	
	
	/*
	 
	private static Alarme findAlarm(int id_horaire, ArrayList<Alarme> alarms) {
		for (Alarme a : alarms)
			if (a.getId_horaire() == id_horaire)
				return a;
		return null;
	}
	
	private int idBusStop;	
	private BusTimeArrayAdapter adapter;
	private FragmentManager manager;
	private boolean isFavorite = false;

	// ----- LISTENER -----
	public OnClickListener setAlarmClickListener = new OnClickListener() {
		public void onClick(View view) {

			SQLiteDatabase db = getDatabase();
			Periode p = PeriodeDAO.select(m_periodeId, db);
			db.close();

			String[] dayNames = view.getContext().getResources().getStringArray(AlarmUtility.getDaysForPeriode(p.nom));
			int index = (Integer) view.getTag(R.id.row_position_key);
			BusTimeRow row = adapter.get(index);
			AlarmConfigFromBusDialog dialog = new AlarmConfigFromBusDialog(view, dayNames, row, adapter);
			dialog.show(manager, "AlarmConfiguration");
		}
	};

	private class ConfirmDeleteClickListener implements DialogInterface.OnClickListener {
		private final BusTimeRow row;
		private final int index;
		private final View sourceView;

		private ConfirmDeleteClickListener(BusTimeRow row, int index, View sourceView) {
			this.row = row;
			this.index = index;
			this.sourceView = sourceView;
		}

		public void onClick(DialogInterface dialog, int which) {
			final Context context = sourceView.getContext();

			SQLiteDatabase db = getDatabase();
			Alarme a = row.getAlarm();
			AlarmeDAO.delete(a.getId(), db);
			db.close();

			AlarmUtility.cancelAlarm(a.getId(), context);
			BusTimeRow newRow = new BusTimeRow(null, row.getHoraire(), row.getFormattedTime());
			adapter.edit(index, newRow);
			adapter.notifyDataSetChanged();
		}
	}

	public OnLongClickListener deleteAlarmClickListener = new OnLongClickListener() {
		public boolean onLongClick(View view) {
			final int index = (Integer) view.getTag(R.id.row_position_key);
			final BusTimeRow row = adapter.get(index);
			if (row.getAlarm() != null) {
				final View sourceView = view;
				AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
				builder.setMessage(R.string.ConfirmDeleteAlarm).setPositiveButton(R.string.ButtonOk, new ConfirmDeleteClickListener(row, index, sourceView))
						.setNegativeButton(R.string.ButtonCancel, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						}).show();
				return true;
			}
			return false;
		}
	};

	
*/


